package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**新增分类
     *
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类{}",categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();
    }

    /**分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

//    /**分类型分页查询
//     *
//     * @param type
//     * @return
//     */
//    @GetMapping("/list")
//    public Result typePageQuery(String type){
//        log.info("分类型分页查询{}",type);
//        Category category = categoryService.pageQueryByType(type);
//        return Result.success(category);
//    }

    /**根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(Long id){
        log.info("根据id删除分类{}",id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**修改分类
     *
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类{}",categoryDTO);
        categoryService.updateById(categoryDTO);
        return Result.success();
    }

    /**启用|禁用分类
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result startOrStopCategory(@PathVariable Integer status,Long id){
        log.info("启用|禁用分类{}",id);
        categoryService.startOrStop(id,status);
        return Result.success();
    }

    /**根据类型查询
     *
     * @param type
     * @return
     */
    public Result<List<Category>> selectByType(Integer type){
        log.info("根据类型查询：{}",type);
        return Result.success(categoryService.selectByTypr(type));
    }
}
